home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch5 / WordWrap.frm (.txt) < prev    next >
Visual Basic Form  |  1999-04-10  |  5KB  |  145 lines

  1. VERSION 5.00
  2. Begin VB.Form frmWordWrap 
  3.    Caption         =   "WordWrap"
  4.    ClientHeight    =   4230
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   6270
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   8.25
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    LinkTopic       =   "Form1"
  18.    ScaleHeight     =   4230
  19.    ScaleWidth      =   6270
  20.    StartUpPosition =   3  'Windows Default
  21.    Begin VB.CommandButton cmdPrint 
  22.       Caption         =   "Print"
  23.       Height          =   495
  24.       Left            =   2520
  25.       TabIndex        =   1
  26.       Top             =   3240
  27.       Width           =   1215
  28.    End
  29.    Begin VB.TextBox txtLongText 
  30.       Height          =   2655
  31.       Left            =   120
  32.       MultiLine       =   -1  'True
  33.       ScrollBars      =   2  'Vertical
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   4455
  37.    End
  38. Attribute VB_Name = "frmWordWrap"
  39. Attribute VB_GlobalNameSpace = False
  40. Attribute VB_Creatable = False
  41. Attribute VB_PredeclaredId = True
  42. Attribute VB_Exposed = False
  43. Option Explicit
  44. ' Print a string on a Printer or PictureBox,
  45. ' wrapped within the margins.
  46. Private Sub PrintWrappedText(ByVal txt As String, ByVal indent As Single, ByVal left_margin As Single, ByVal top_margin As Single, ByVal right_margin As Single, ByVal bottom_margin As Single)
  47. Dim next_paragraph As String
  48. Dim next_word As String
  49. Dim pos As Integer
  50.     ' Start at the top of the page.
  51.     Printer.CurrentY = top_margin
  52.     ' Repeat until the text is all printed.
  53.     Do While Len(txt) > 0
  54.         ' Get the next paragraph.
  55.         pos = InStr(txt, vbCrLf)
  56.         If pos = 0 Then
  57.             ' Use the rest of the text.
  58.             next_paragraph = Trim$(txt)
  59.             txt = ""
  60.         Else
  61.             ' Get the paragraph.
  62.             next_paragraph = Trim$(Left$(txt, pos - 1))
  63.             txt = Mid$(txt, pos + Len(vbCrLf))
  64.         End If
  65.         ' Indent the paragraph.
  66.         Printer.CurrentX = left_margin + indent
  67.         ' Print the paragraph.
  68.         Do While Len(next_paragraph) > 0
  69.             ' Get the next word.
  70.             pos = InStr(next_paragraph, " ")
  71.             If pos = 0 Then
  72.                 ' Use the rest of the paragraph.
  73.                 next_word = next_paragraph
  74.                 next_paragraph = ""
  75.             Else
  76.                 ' Get the word.
  77.                 next_word = Left$(next_paragraph, pos - 1)
  78.                 next_paragraph = Trim$(Mid$(next_paragraph, pos + 1))
  79.             End If
  80.             ' See if there is room for this word.
  81.             If Printer.CurrentX + Printer.TextWidth(next_word) _
  82.                 > right_margin _
  83.             Then
  84.                 ' It won't fit. Start a new line.
  85.                 Printer.Print
  86.                 Printer.CurrentX = left_margin
  87.                 ' See if we have room for a new line.
  88.                 If Printer.CurrentY + Printer.TextHeight(next_word) _
  89.                     > bottom_margin _
  90.                 Then
  91.                     ' Start a new page.
  92.                     Printer.NewPage
  93.                     Printer.CurrentX = left_margin
  94.                     Printer.CurrentY = top_margin
  95.                 End If
  96.             End If
  97.             ' Now print the word. The ; makes the
  98.             ' Printer not move to the next line.
  99.             Printer.Print next_word & " ";
  100.         Loop
  101.         ' Finish the paragraph by ending the line.
  102.         Printer.Print
  103.     Loop
  104. End Sub
  105. ' Print the text.
  106. Private Sub cmdPrint_Click()
  107.     Screen.MousePointer = vbHourglass
  108.     ' Select a big font so we have more than one page.
  109.     Printer.Font.Size = 20
  110.     ' Print the text.
  111.     PrintWrappedText txtLongText.Text, _
  112.         720, 1440, 1440, _
  113.         Printer.ScaleWidth - 1440, _
  114.         Printer.ScaleHeight - 1440
  115.     Printer.EndDoc
  116.     Screen.MousePointer = vbDefault
  117. End Sub
  118. Private Sub Form_Load()
  119. Dim fname As String
  120. Dim fnum As Integer
  121. Dim txt As String
  122.     fname = App.Path
  123.     If Right$(fname, 1) <> "\" Then fname = fname & "\"
  124.     fname = fname & "longtext.txt"
  125.     On Error Resume Next
  126.     fnum = FreeFile
  127.     Open fname For Input As fnum
  128.     txt = Input$(LOF(fnum), fnum)
  129.     Close fnum
  130.     txtLongText.Text = txt
  131. End Sub
  132. ' Make the TextBox as big as possible.
  133. Private Sub Form_Resize()
  134. Dim wid As Single
  135. Dim hgt As Single
  136.     wid = ScaleWidth - 2 * 120
  137.     If wid < 120 Then wid = 120
  138.     hgt = ScaleHeight - cmdPrint.Height - 3 * 120
  139.     If hgt < 120 Then hgt = 120
  140.     txtLongText.Move 120, 120, wid, hgt
  141.     cmdPrint.Move _
  142.         (ScaleWidth - cmdPrint.Width) / 2, _
  143.         ScaleHeight - cmdPrint.Height - 120
  144. End Sub
  145.